package com.udaos.modules.wx.services;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;

import com.udaos.common.base.Service;
import com.udaos.common.util.DateUtil;
import com.udaos.models.wx.Wx_config;
import com.udaos.models.wx.Wx_tpl_list;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;

/**
 * 微信模板消息Service
 * @author mutaotao
 *
 */
@IocBean
public class ApiWxTemplateService extends Service{
	
	@Inject
	private WxTplListService listService;
	@Inject
	private WxConfigService wxConfigService;
	@Inject
	protected PropertiesProxy app;
	/**
	 * 发送体检码模板消息
	 * @param wxId     	微信APPID
	 * @param openId	用户openId
	 * @param code		体检码
	 * @return
	 */
	public Boolean sendPhysicalTemplate(String wxId,String openId,String code){
		WxMpService wxMpService=getWxMpService(wxId);
		//获取发送体检码模板
		//获取微信
		Wx_config config=wxConfigService.fetch(Cnd.where("appid", "=", wxId));
		Cnd cnd =Cnd.NEW();
		cnd.and("title", "=", "体检码通知");
		cnd.and("wxid", "=", config.getId());
		Wx_tpl_list tpl=listService.fetch(cnd);
		String physical_id=tpl.getTemplate_id();
		//组装数据
		WxMpTemplateMessage templateMessage=new WxMpTemplateMessage();
		templateMessage.setTemplateId(physical_id);
		templateMessage.setToUser(openId);
		templateMessage.setUrl(app.get("udaos.weixin.root") + "/enroll/toPhysical");
		System.out.println(templateMessage.getUrl()+"==================");
		List<WxMpTemplateData> dataList=new ArrayList<WxMpTemplateData>();
		WxMpTemplateData data=new WxMpTemplateData();
		data.setName("first");
		data.setColor("#173177");
		data.setValue("请携带身份证原件，到就近体检站进行体检");
		WxMpTemplateData data1=new WxMpTemplateData();
		data1.setName("keyword1");
		data1.setValue(code);
		data1.setColor("#173177");
		WxMpTemplateData data2=new WxMpTemplateData();
		data2.setName("keyword2");
		data2.setValue(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm"));
		data2.setColor("#173177");
		WxMpTemplateData data3=new WxMpTemplateData();
		data3.setName("remark");
		data3.setValue("1.体检前请务必确定报考准驾车型(C1或C2)，确定后不能更改\n2.体检时，向工作人员出示体检码，切勿将体检码泄露给他人。点击查看体检站分布");
		data3.setColor("#173177");
		dataList.add(data);
		dataList.add(data1);
		dataList.add(data2);
		dataList.add(data3);
		
		templateMessage.setData(dataList);
		
		try {
			wxMpService.templateSend(templateMessage);
			return true;
		} catch (WxErrorException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 提现到账通知
	 * @param app_id
	 * @param open_id
	 * @param d
	 * @param integer
	 * @param integer2
	 * @return
	 */
	public boolean sendTxTemplate(String app_id, String open_id, double out_now_money, Integer create_time, Integer pay_time,Integer tx_type) {
		WxMpService wxMpService=getWxMpService(app_id);
		//获取发送体检码模板
		//获取微信
		Wx_config config=wxConfigService.fetch(Cnd.where("appid", "=", app_id));
		Cnd cnd =Cnd.NEW();
		cnd.and("title", "=", "提现成功通知");
		cnd.and("wxid", "=", config.getId());
		Wx_tpl_list tpl=listService.fetch(cnd);
		String physical_id=tpl.getTemplate_id();
		//组装数据
		WxMpTemplateMessage templateMessage=new WxMpTemplateMessage();
		templateMessage.setTemplateId(physical_id);
		templateMessage.setToUser(open_id);
		List<WxMpTemplateData> dataList=new ArrayList<WxMpTemplateData>();
		WxMpTemplateData data=new WxMpTemplateData();
		data.setName("first");
		data.setColor("#173177");
		data.setValue("恭喜您提现成功！");
		WxMpTemplateData data1=new WxMpTemplateData();
		data1.setName("keyword1");
		data1.setValue(out_now_money+"");
		data1.setColor("#173177");
		WxMpTemplateData data2=new WxMpTemplateData();
		data2.setName("keyword2");
		String txType="";
		if(tx_type==0){
			txType="微信钱包";
		}else if(tx_type==1){
			txType="银行卡";
		}
		data2.setValue(txType);
		data2.setColor("#173177");
		WxMpTemplateData data3=new WxMpTemplateData();
		data3.setName("keyword3");
		data3.setValue(DateUtil.getDate(create_time, "yyyy-MM-dd HH:mm"));
		data3.setColor("#173177");
		WxMpTemplateData data4=new WxMpTemplateData();
		data4.setName("keyword4");
		data4.setValue(DateUtil.getDate(pay_time, "yyyy-MM-dd HH:mm"));
		data4.setColor("#173177");
		WxMpTemplateData data5=new WxMpTemplateData();
		data5.setName("keyword5");
		data5.setValue("申请金额已到账，请注意查收");
		data5.setColor("#173177");
		dataList.add(data);
		dataList.add(data1);
		dataList.add(data2);
		dataList.add(data3);
		dataList.add(data4);
		dataList.add(data5);
		
		templateMessage.setData(dataList);
		
		try {
			wxMpService.templateSend(templateMessage);
			return true;
		} catch (WxErrorException e) {
			e.printStackTrace();
		}
		return false;
	}
	/**
	 * 获取微信公众号服务
	 * 
	 * @param wxid
	 * @return
	 */
	public WxMpService getWxMpService(String wxid) {
		WxMpConfigStorage config = getWxConfig(wxConfigService.fetch(Cnd.where("appid", "=", wxid)));
		WxMpService wxMpService = new WxMpServiceImpl();
		wxMpService.setWxMpConfigStorage(config);
		return wxMpService;
	}
	/**
	 * 获取公众号配置
	 * 
	 * @param appInfo
	 * @return
	 */
	public WxMpConfigStorage getWxConfig(Wx_config appInfo) {
		if (null == appInfo)
			return null;
		WxMpConfigStorage config = new WxMpInMemoryConfigStorage();
		((WxMpInMemoryConfigStorage) config).setAppId(appInfo.getAppid()); // 设置微信公众号的appid
		((WxMpInMemoryConfigStorage) config).setSecret(appInfo.getAppsecret()); // 设置微信公众号的app
		((WxMpInMemoryConfigStorage) config).setToken(appInfo.getToken()); // 设置微信公众号的token
		((WxMpInMemoryConfigStorage) config).setAesKey(appInfo.getEncodingAESKey()); // 设置微信公众号的EncodingAESKey

		return config;
	}

	public Boolean sendTaskRemind(String wxid, String receiverOpenid, String url,String first, String keyword1, String keyword2, String keyword3, String remark){
		WxMpService wxMpService=getWxMpService(wxid);
		//获取微信
		Wx_config config=wxConfigService.fetch(Cnd.where("appid", "=", wxid));
		Cnd cnd =Cnd.NEW();
		cnd.and("title", "=", "待办事项提醒");
		cnd.and("wxid", "=", config.getId());
		Wx_tpl_list tpl=listService.fetch(cnd);
		String physical_id=tpl.getTemplate_id();
		//组装数据
		WxMpTemplateMessage templateMessage=new WxMpTemplateMessage();
		templateMessage.setTemplateId(physical_id);
		templateMessage.setToUser(receiverOpenid);
		if(!Strings.isBlank(url)){
			templateMessage.setUrl(url);
		}
		List<WxMpTemplateData> dataList=new ArrayList<WxMpTemplateData>();
		WxMpTemplateData data=new WxMpTemplateData();
		data.setName("first");
		data.setColor("#173177");
		data.setValue(first);
		WxMpTemplateData data1=new WxMpTemplateData();
		data1.setName("keyword1");
		data1.setValue(keyword1);
		data1.setColor("#173177");
		WxMpTemplateData data4=new WxMpTemplateData();
		data4.setName("keyword2");
		data4.setValue(keyword2);
		data4.setColor("#173177");
		WxMpTemplateData data2=new WxMpTemplateData();
		data2.setName("keyword3");
		data2.setValue(keyword3);
		data2.setColor("#173177");
		WxMpTemplateData data3=new WxMpTemplateData();
		data3.setName("remark");
		data3.setValue(remark);
		data3.setColor("#173177");
		dataList.add(data);
		dataList.add(data1);
		dataList.add(data2);
		dataList.add(data3);
		dataList.add(data4);
		
		templateMessage.setData(dataList);
		
		try {
			wxMpService.templateSend(templateMessage);
			return true;
		} catch (WxErrorException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 发送分配教练完成信息
	 * @param appid
	 * @param openId
	 * @param name
	 * @param address
	 * @return
	 */
	public boolean sendFpjlTemplate(String wxid, String openId, String name, String address,String url,String mobile) {
		WxMpService wxMpService=getWxMpService(wxid);
		//获取微信
		Wx_config config=wxConfigService.fetch(Cnd.where("appid", "=", wxid));
		Cnd cnd =Cnd.NEW();
		cnd.and("title", "=", "教练分配通知");
		cnd.and("wxid", "=", config.getId());
		Wx_tpl_list tpl=listService.fetch(cnd);
		String physical_id=tpl.getTemplate_id();
		//组装数据
		WxMpTemplateMessage templateMessage=new WxMpTemplateMessage();
		templateMessage.setTemplateId(physical_id);
		templateMessage.setToUser(openId);
		if(!Strings.isBlank(url)){
			templateMessage.setUrl(url);
		}
		List<WxMpTemplateData> dataList=new ArrayList<WxMpTemplateData>();
		WxMpTemplateData data=new WxMpTemplateData();
		data.setName("first");
		data.setColor("#173177");
		data.setValue("您好，已经为您分配教练");
		WxMpTemplateData data1=new WxMpTemplateData();
		data1.setName("keyword1");
		data1.setValue(name);
		data1.setColor("#173177");
		WxMpTemplateData data4=new WxMpTemplateData();
		data4.setName("keyword2");
		data4.setValue(mobile);
		data4.setColor("#173177");
		WxMpTemplateData data2=new WxMpTemplateData();
		data2.setName("keyword3");
		data2.setValue(address);
		data2.setColor("#173177");
		WxMpTemplateData data3=new WxMpTemplateData();
		data3.setName("remark");
		data3.setValue("感谢您的使用");
		data3.setColor("#173177");
		dataList.add(data);
		dataList.add(data1);
		dataList.add(data2);
		dataList.add(data3);
		dataList.add(data4);
		
		templateMessage.setData(dataList);
		
		try {
			wxMpService.templateSend(templateMessage);
			return true;
		} catch (WxErrorException e) {
			e.printStackTrace();
		}
		return false;
	}



	
}
